
$(function () {
    'use strict';
    $(".ksr .fileupload").each(function(){
    	var that = this;
    	$(that).fileupload({
    		url:$(that).attr("uploadUrl"),
    		dropZone: $(that).find(".drop-zone"),
    		maxFileSize: 5000000,
    		// singleFileUploads:false,
    		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i
    	});
    	$(that).bind('fileuploadsubmit', function (e, data) {
    	    data.formData = {entityId: $(that).find("input[id='entityId']").val()};
//    	    console.log("on submit: primary="+data.formData.primary);
//    	    if (!data.formData.example) {
//    	      data.context.find('button').prop('disabled', false);
//    	      input.focus();
//    	      return false;
//    	    }
//    	    return false;
    	});
    	
    	$(that).on("change","input[name^='primary']",function(){
    		var self = this;
    		$.ajax({
                // Uncomment the following to send cross-domain cookies:
                //xhrFields: {withCredentials: true},
                url: $(self).attr("setPrimaryUrl"),
                dataType: 'json',
                dropZone: $(that).find(".drop-zone"),
                context: $(that)
            }).always(function () {
                
            }).done(function (result) {
                console.log("set primary call back: result="+result.isSetPrimary);
            });
    	});
    	
    	var getFilesUrl = $(that).attr("get-picture-url");
    	if (typeof getFilesUrl!=="undefined"){
    		$.ajax({
                // Uncomment the following to send cross-domain cookies:
                //xhrFields: {withCredentials: true},
                url: getFilesUrl,
                dataType: 'json',
                dropZone: $(that).find(".drop-zone"),
                context: $(that)
            }).always(function () {
                $(this).removeClass('fileupload-processing');
            }).done(function (result) {
            	console.log("get file json is done, with url="+getFilesUrl)
                $(this).fileupload('option', 'done')
                    .call(this, $.Event('done'), {result: result});
            });
    	}
    })
    disableDropzone();
});

function enableFileupload(arg){//arg="disable" or "enable"
	$(".ksr .fileupload").fileupload(arg);
}
function enableDropzone(){
	console.log("enable dropzone")
	$(".ksr .fileupload").each(function(){
		var that = this;
		$(that).fileupload(
				'option',
				{
					dropZone:$(that).find(".drop-zone")
				}
		);
	});
    $(document).bind('dragover', function (e)
			{
			    var dropZone = $('.ksr .drop-zone'),
			        foundDropzone,
			        timeout = window.dropZoneTimeout;
			        if (!timeout)
			        {
			            dropZone.addClass('in');
			        }
			        else
			        {
			            clearTimeout(timeout);
			        }
			        var found = false,
			        node = e.target;

			        do{

			            if ($(node).hasClass('drop-zone'))
			            {
			                found = true;
			                foundDropzone = $(node);
			                break;
			            }

			            node = node.parentNode;

			        }while (node != null);

			        dropZone.removeClass('in hover');

			        if (found)
			        {
			            foundDropzone.addClass('hover');
			        }

			        window.dropZoneTimeout = setTimeout(function ()
			        {
			            window.dropZoneTimeout = null;
			            dropZone.removeClass('in hover');
			        }, 100);
			    });
	$(document).bind('drop dragover', function (e) {
	    e.preventDefault();
	});	
}
function disableDropzone(){
	console.log("disable dropzone")
	$(".ksr .fileupload").each(function(){
		var that = this;
		$(that).fileupload(
				'option',
				{
					dropZone:null
				}
		);
	});
	$(document).unbind('dragover');
	$(document).unbind('drop dragover');
	$(document).bind('drop dragover', function (e) {
	    e.preventDefault();
	});	
}